package com.example.app05.activity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.app05.MainActivity;
import com.example.app05.R;
import com.example.app05.dao.MyDatabaseHelper;
import com.example.app05.dao.TaskDao;
import com.example.app05.entity.Task;
import com.example.app05.tool.Tool;

import java.text.SimpleDateFormat;

/**
 * @author: cx
 * @date: 2022/5/1
 */
public class AddActivity extends AppCompatActivity {
    //创建数据库所用参数
    private SQLiteDatabase db;
    private MyDatabaseHelper dbHelper;

    //页面控件
    private Button btnReturn, btnAdd;
    private EditText etTopic, etDoTime, etContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_add);

        initDatabaseByDBHelper();

        findView();
        setListener();
    }

    //创建数据库
    public void initDatabaseByDBHelper() {
        //创建数据库管理类的对象
        dbHelper = new MyDatabaseHelper(
                this,"task_table",null,1
        );
        //获得数据库对象
        db = dbHelper.getWritableDatabase();
    }

    private void findView() {
        btnReturn = findViewById(R.id.ta_btnBack);
        btnAdd = findViewById(R.id.ta_btnAdd);
        etTopic = findViewById(R.id.ta_topic);
        etDoTime = findViewById(R.id.ta_time);
        etContent = findViewById(R.id.ta_content);
    }

    private void setListener(){
        //点击返回按钮返回到列表页面
        btnReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tool.onItemClick(AddActivity.this, MainActivity.class,null);
                AddActivity.this.finish();
            }
        });
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String showTime = sdf.format(System.currentTimeMillis());
                String newTime = sdf.format(System.currentTimeMillis());
                Task task = new Task(etTopic.getText().toString(),etDoTime.getText().toString(),showTime,newTime,etContent.getText().toString());
                Log.e("cx",task.toString());
                long dataId = TaskDao.insertData(db,task);
                if(dataId > 0){
                    Toast.makeText(
                            getApplicationContext(),
                            "插入数据成功",
                            Toast.LENGTH_SHORT
                    ).show();
                    //添加成功回到主页面
                    Tool.onItemClick(AddActivity.this,MainActivity.class,null);
                    AddActivity.this.finish();
                }else{
                    Toast.makeText(
                            getApplicationContext(),
                            "插入数据失败",
                            Toast.LENGTH_SHORT
                    ).show();
                }
            }
        });
    }
}
